package com.lanou3g.mynewstest.module.news.comic;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.lanou3g.mynewstest.R;
import com.lanou3g.mynewstest.base.MyBaseActivity;
import com.lanou3g.mynewstest.beans.ComicIndexBeans;
import com.lanou3g.mynewstest.tools.MyRequestQueue;
import com.lanou3g.mynewstest.tools.StringRequestUtf;

/**
 * Created by dllo on 16/1/26.
 */
public class ComicIndexActivity extends MyBaseActivity {
    private String url = "";
    private String postId = "";
    private ComicIndexBeans bean;
    private WebView webView;
    private Handler handler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_comic_index);
        webView = (WebView) findViewById(R.id.comic_web_view);
        Intent intent = getIntent();
        postId = intent.getStringExtra("postId");
        getBean(getUrl(postId));

    }

    private void getBean(String url) {
        StringRequestUtf requestUtf = new StringRequestUtf(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                response = response.replaceFirst(postId, "BE8C4ED8002164A0");
                Gson gson = new Gson();
                bean = gson.fromJson(response, ComicIndexBeans.class);
                String href = bean.getBE8C4ED8002164A0().getLink().get(0).getHref();
//                href = href.substring(href.lastIndexOf("/"), href.length());
//                href = href.replace("/", "");
//                init(href);
                init();

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        MyRequestQueue.addRequest(requestUtf);
    }

    private String getUrl(String postId) {
        url = "http://c.m.163.com/nc/article/" + postId + "/full.html";
        Log.d("ComicIndexActivity", url);
        return url;
    }

    private String getHref(String href) {
        href = "http://h5.manhua.163.com/book/catalog/" + href + ".json";
        return href;
    }

    private void init(String href) {
        href = "http://h5.manhua.163.com/source/" + href;
        Log.d("ComicIndexActivity", href);
        WebSettings webSettings = webView.getSettings();
        webSettings.setJavaScriptEnabled(true);


        webView.requestFocusFromTouch();
        //WebView加载web资源
        webView.loadUrl(href);
        //覆盖WebView默认使用第三方或系统默认浏览器打开网页的行为，使网页用WebView打开
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                // TODO Auto-generated method stub
                //返回值是true的时候控制去WebView打开，为false调用系统浏览器或第三方浏览器
                view.loadUrl(url);


                return false;
            }
        });
    }

    private void init() {

        WebSettings webSettings = webView.getSettings();
        webSettings.setJavaScriptEnabled(true);


        webView.requestFocusFromTouch();
        //WebView加载web资源
        webView.loadUrl(bean.getBE8C4ED8002164A0().getLink().get(0).getHref());
        //覆盖WebView默认使用第三方或系统默认浏览器打开网页的行为，使网页用WebView打开
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                // TODO Auto-generated method stub
                //返回值是true的时候控制去WebView打开，为false调用系统浏览器或第三方浏览器
                view.loadUrl(url);


                return false;
            }
        });
    }
}
